

/**
 *  
 * @param H 0 ≤ H < 360
 * @param S 0 ≤ S ≤ 1
 * @param L 0 ≤ L ≤ 1
 * @description C = (1 - |2L - 1|) × S
        X = C × (1 - |(H / 60°) mod 2 - 1|)
        m = L - C/2
 */
export function HSL2RGB(H: number, S: number, L: number) {
    // console.log(H, S, L);

    const C: number = (1 - Math.abs(2 * L - 1)) * S,
        X: number = C * (1 - Math.abs((H / 60) % 2 - 1)),
        m = L - C / 2;
    let _R = 0,
        _G = 0,
        _B = 0;
    if (H >= 0 && H < 60) {
        [_R, _G, _B] = [C, X, 0];
    } else if (H >= 60 && H < 120) {
        [_R, _G, _B] = [X, C, 0];
    } else if (H >= 120 && H < 180) {
        [_R, _G, _B] = [0, C, X];
    } else if (H >= 180 && H < 240) {
        [_R, _G, _B] = [0, X, C];
    } else if (H >= 240 && H < 300) {
        [_R, _G, _B] = [X, 0, C];
    } else if (H >= 300 && H < 360) {
        [_R, _G, _B] = [C, 0, X];
    }
    const RGB = [Math.round((_R + m) * 255),
    Math.round((_G + m) * 255),
    Math.round((_B + m) * 255)];
    const hex = [
        getHex(RGB[0]),
        getHex(RGB[1]),
        getHex(RGB[2])
    ];
    // console.log(hex);
    return {
        decimal: RGB,
        hex: `#${hex[0]}${hex[1]}${hex[2]}`
    }
}
function getHex(num: number) {
    let result = num.toString(16);
    if (result == "0") {
        result = "00";
    } else if (result.length == 1) {
        result = '0' + result;
    }
    return result;
}